//
//  CLFile.m
//  FileSystemDemo
//
//  Created by 小草毋正 on 2018/1/8.
//  Copyright © 2018年 C-LOVE. All rights reserved.
//

#import "CLFile.h"


@implementation CLFile


- (instancetype)initWithFilePath:(NSString *)filePath {
    self = [super init];
    if (self) {
        self.filePath = filePath;
        self.name = [filePath lastPathComponent];
        
        //判断是否为文件夹
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isDirectory;
        BOOL exist = [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
        self.fileInfo = [[CLFileInfo alloc] initWithFilePath:filePath];
        if(exist == NO){
            
        }
        else {
            if(isDirectory){
                self.fileType = CLFileTypeFolder;
            }
            else {
                NSString *extension = [[filePath pathExtension] uppercaseString];
                if([[self imageExtensions] containsObject:extension]){
                    self.fileType = CLFileTypeImage;
                }
                else if ([[self musicExtensions] containsObject:extension]){
                    self.fileType = CLFileTypeMusic;
                }
                else if ([[self lrcExtensions] containsObject:extension]){
                    self.fileType = CLFileTypeLRC;
                }
                else if ([[self readmeExtensions] containsObject:extension]){
                    self.fileType = CLFileTypeMD;
                }
                else if ([[self codeExtensions] containsObject:extension]){
                    self.fileType = CLFileTypeCode;
                }
                else {
                    self.fileType = CLFileTypeOther;
                }
            }
        } 
    }
    return self;
} 


- (void)setFileType:(CLFileType)fileType {
    _fileType = fileType;
    switch (fileType) {
        case CLFileTypeDefault: {
            self.image = [UIImage imageNamed:@"default"];
        }
            break;
        case CLFileTypeFolder: {
            self.image = [UIImage imageNamed:@"folder"];
        }
            break;
        case CLFileTypeMusic: {
            self.image = [UIImage imageNamed:@"music"];
        }
            break;
        case CLFileTypeTxt: {
            self.image = [UIImage imageNamed:@"txt"];
        }
            break;
        case CLFileTypeVideo: {
            self.image = [UIImage imageNamed:@"video"];
        }
            break;
        case CLFileTypeImage: {
            self.image = [UIImage imageNamed:@"image"];
        }
            break;
        case CLFileTypeLRC: {
            self.image = [UIImage imageNamed:@"lrc"];
        }
            break;
        case CLFileTypeOther: {
            self.image = [UIImage imageNamed:@"other"];
        }
            break;
        case CLFileTypeMD: {
            self.image = [UIImage imageNamed:@"readMe"];
        }
            break;
        case CLFileTypeCode: {
            self.image = [UIImage imageNamed:@"code"];
        }
            break;
            
            //readMe
        default:
            break;
    }
}



- (NSArray *)imageExtensions {
    return @[@"PNG", @"JPG", @"JPEG" ,@"gif"];
}

- (NSArray<NSString *> *)musicExtensions {
    return @[@"MP3", @"WMA", @"M4A", @"AAC"];
}

- (NSArray<NSString *> *)lrcExtensions {
    return @[@"LRC"];
}

- (NSArray<NSString *> *)readmeExtensions {
    return @[@"MD"];
}

- (NSArray<NSString *> *)codeExtensions {
    return @[@"H", @"M"];
}


@end


